﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XKJ.OAWeb.Infrastructrue;
using XKJ.OAWeb.Models;
using XKJ.OAWeb.Shared;

namespace XKJ.OAWeb.Application
{
    public class NavigationService : INavigationService
    {
        IBaseRepository<Navigation> _navigation;

        public NavigationService(IBaseRepository<Navigation> navigation)
        {
            _navigation = navigation;
        }

        public object? GetList()
        {
            //查询所有的数据
            var data = _navigation.Show();

            int pid = 0;

            if (data!=null)
            {
                //调用递归方法
                return this.GetItem(data.ToList(), pid);
            }
            else
            {
                return null;
            }
        }
        //递归方法
        private List<NavigationDto>? GetItem(List<Navigation> navigations, int pid)
        {
            //实例化一个集合
            List<NavigationDto> Itemdata = new List<NavigationDto>();
            //获取一级索引数据
            var listdata = navigations.Where(x => x.Pid == pid).ToList();
            //循环遍历
            listdata.ForEach(x =>
            {
                if (navigations.Where(y=>y.Pid==x.NId).Count()<0)
                {
                    Itemdata.Add(new NavigationDto
                    {
                        NId=x.NId,
                        NavName=x.NavName,
                        Pid=x.Pid
                    });
                }
                else
                {
                    Itemdata.Add(new NavigationDto
                    {
                        NId = x.NId,
                        NavName = x.NavName,
                        Pid = x.Pid,
                        children=GetItem(navigations,x.NId)
                    });
                }
            });
            return Itemdata;

        }
    }
}
